<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <button id="purchaseTypeBtnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
                        <!--<button id="purchaseTypeBtnExport" class="layui-btn icon-btn"><i class="layui-icon">&#xe67d;</i>导出</button>-->
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="purchaseTypeTable" lay-filter="purchaseTypeTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="purchaseTypeTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="purchaseTypeForm">
    <form lay-filter="purchaseTypeForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">名称</label>
            <div class="layui-input-block">
                <input name="purchaseTypeName" placeholder="请输入采购类型名称" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">编码头</label>
            <div class="layui-input-block">
                <input name="typeHeadCode" placeholder="请输入类型编码头" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">责任部门</label>
            <div class="layui-input-block">
                <input id="departmentId" type="hidden" name="departmentId"/>
                <div id="selectDepartment"></div>

            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">审核岗位</label>
            <div class="layui-input-block">
                <select name="stationsId" id="stationsId" lay-filter="stationsId" lay-verify="required" required></select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">描述</label>
            <div class="layui-input-block">
                <input name="remark" placeholder="请输入描述" type="text" class="layui-input"  required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">是否启用</label>
            <div class="layui-input-block">
                <input name="isDeleted" type="checkbox" lay-skin="switch" value="1" checked="false" lay-text="启用|停用"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">排序</label>
            <div class="layui-input-block">
                <input name="sortNumber" placeholder="数字，数字越小，排序越靠前" type="text" class="layui-input"  lay-verify="intNum" required/>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="purchaseTypeFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;

        //整数校验
        form.verify({
            intNum: [
                /^-?[0-9]\d*$/
                ,'请输入整数'
            ]
        });

        // 渲染表格
        var insTb = table.render({
            elem: '#purchaseTypeTable',
            url: config.base + 'enterprise/purchase/admin/purchaseType/list',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers', title: '序号'}
                ,{field: 'purchaseTypeName',align: 'center', title: '名称'}
                ,{field: 'typeHeadCode',align: 'center', title: '编码头'}
                ,{field: 'remark', align: 'center', title: '描述'}
                ,{field: 'department', align: 'center', title: '责任部门'}
                ,{field: 'stations', align: 'center', title: '审核岗位'}
                ,{field: 'isDeleted', align: 'center', title: '是否启用',templet:function (d) {
                        if(d.isDeleted){return '启用';}else{return '未启用';}
                    }
                }
                ,{field: 'creatorName',align: 'center', title: '创建人'}
                ,{field: 'createTime',align: 'center', title: '创建时间'}
                ,{align: 'center', toolbar: '#purchaseTypeTableBar', title: '操作', width: 200}
            ]],
        });

        // 工具条点击事件
        table.on('tool(purchaseTypeTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }
        });

        //监听排序
        table.on('sort(purchaseTypeTable)', function(obj){
            table.reload('purchaseTypeTable', {
                initSort: obj
                ,where: {
                    sort: obj.field
                    ,order: obj.type
                }
            });
        });

        // 添加按钮点击事件
        $('#purchaseTypeBtnAdd').click(function () {
            showEditModel();
        });

        // 显示编辑弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: '660px',
                offset: '65px',
                title: data ? '修改采购类型' : '添加采购类型',
                content: $('#purchaseTypeForm').html(),
                success: function () {
                    var departmentName, stationsName;
                    //部门列表
                    admin.initDataSelectTree(true, 'user/admin/role/getTreeData', null, 'selectDepartment', data ? data.departmentId : null, "部门", "departmentId", function (data) {
                        console.info(data.change[0]);
                        // 获取部门信息
                        let departmentId = data.change[0].value;
                        departmentName = data.change[0].name;
                        //赋值部门id
                        $('#departmentId').val(data.change[0].value);
                        $('#stationsId').empty();
                        //部门下的岗位列表
                        admin.initDataSelect("user/admin/role/getPostData", {roleId: departmentId}, "stationsId", null, "岗位", null, null);
                    });
                    //编辑-初始化岗位列表
                    if(data){
                        admin.initDataSelect("user/admin/role/getPostData", {roleId: data.departmentId}, "stationsId", data ? data.stationsId : null, "岗位", null, null);
                    }
                    //岗位选择事件
                    form.on('select(stationsId)', function (data) {
                        stationsName =  data.elem[data.elem.selectedIndex].text;
                    });

                    form.val('purchaseTypeForm', data);
                    // 表单提交事件
                    form.on('submit(purchaseTypeFormSubmit)', function (d) {
                        layer.load(2);
                        var isDeleted=$("form input[name='isDeleted']").prop("checked");
                        if(!isDeleted){
                            d.field.isDeleted=false;
                        }else{
                            d.field.isDeleted=true;
                        }
                        d.field.department= departmentName;
                        d.field.stations=stationsName;
                        admin.req(data ? 'enterprise/purchase/admin/purchaseType/update' : 'enterprise/purchase/admin/purchaseType/add', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('purchaseTypeTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprise/purchase/admin/purchaseType/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
                insTb.reload();
            });
        }


        // option渲染
        function loadSelectHtml(data, tips, htmltarget, type) {
            let html = ' <option value>' + tips + '</option>';
            if (!data || data == null) {
                return;
            }
            for (let i = 0; i < data.length; i++) {
                if (type === 0) {
                    html += `<option value='${data[i].id}'>${data[i].name}</option>`;
                } else if (type === 1) {
                    html += `<option value='${data[i].id}'>${data[i].corporateName}</option>`;
                }
            }

            $("form select[name='" + htmltarget + "']").html(html);
            form.render('select');
        }


        // 导出excel
        $('#purchaseTypeBtnExport').click(function () {
            var checkRows = table.checkStatus('purchaseTypeTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

    });
</script>